﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using ZG_member.BLL;
using System.Configuration;
namespace ZG_member
{
    public partial class GoodsChart : Form
    {
        public GoodsChart()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            jiazai();
        }
        public void jiazai(){
            this.timer1.Start();
            cbxDateUnit.Text = "月";

            dtpStart.Value = dtpStart.Value.AddMonths(-1);
            Series s2 = new Series("销售数量");
            chart1.Series.Add(s2);
            goodsType g = new goodsType();
            List<Model.goodsType> list = g.GetModelList("");
            this.cboGoodsType.DataSource = g.GetModelList("");
            this.cboGoodsType.DisplayMember = "typename";
            this.cboGoodsType.ValueMember = "id";
            this.cboGoodsType.SelectedIndex = 0;
            jilian();
            memberInfo mi = new memberInfo();
            DataSet dt2 = mi.GetListSql("select aa.typeName as 类型,sum(aa.金额) as 销售额 from (select gt.typeName,sum(abc.totalcount) as 金额,gid from (select gid,goodsprice,SUM(goodscount) sumgoodscount,goodsprice*SUM(goodscount)*zhekou/10 totalcount from goodsInfo group by gid,goodsprice,zhekou) as abc inner join dbo.goods g on(g.goodsId = gid) inner join dbo.goodsType gt on(gt.id = g.goodsTypeId) group by gid,gt.typeName) as aa group by aa.typeName");
            decimal zonghe = 0;
            for (int i = 0; i < dt2.Tables[0].Rows.Count; i++)
            {
                zonghe = zonghe + decimal.Parse(dt2.Tables[0].Rows[i]["销售额"].ToString());
            }
            List<string> name = new List<string>();
            List<string> bili = new List<string>();
            for (int i = 0; i < dt2.Tables[0].Rows.Count; i++)
            {
                name.Add(dt2.Tables[0].Rows[i]["类型"].ToString());
                string aa = ((100 / zonghe) * decimal.Parse(dt2.Tables[0].Rows[i]["销售额"].ToString())).ToString();
                bili.Add(aa);

            }
            
            //chart2.DataSource = dt2.Tables[0];
            //chart2.Series.RemoveAt(0);
            chart2.Series[0].Points.DataBindXY(name, bili);
            
            //for (int i = 0; i < name.Count; i++)
            //{
            //    Series type = new Series(list[i].typeName);
            //    chart2.Series.Add(type);
            //    //chart2.Series[list[i].typeName].XValueMember = "类型";
            //    //chart2.Series[list[i].typeName].YValueMembers = "销售额";
            //    //chart2.Series[0].Points.DataBindXY("类型", "销售额");
            //    //chart2.Series[list[i].typeName].IsValueShownAsLabel = true;

            //}
            //for (int i = 0; i < name.Count; i++)
            //{
            //    chart2.Series[i].Points.DataBindXY(name[i], bili[i]);
            //}
            chart2.DataBind();
        }
        private void GoodsChart_Load(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void GoodsChart_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }                               
        }
        public void sousuo(){
            goodsInfo memInfo = new goodsInfo();
            string unit = "";
            DataTable dt;
            
            switch (cbxDateUnit.Text)
            {

                case "月":
                    unit = "MM";
                    dt = memInfo.GetList("select  convert(varchar,DATEPART (YYYY , selltime ))+ '年' + convert(varchar,DATEPART (MM , selltime )) + '月' as dateU,sum(goodscount) as 数量 from goodsinfo where gid = " + cbogoods.SelectedValue + " and  selltime>='" + dtpStart.Value + "' and selltime<='" + dtpEnd.Value + "' group by DATEPART (YYYY , selltime ),DATEPART (MM , selltime ) order by DATEPART (YYYY , selltime ),DATEPART (MM , selltime )").Tables[0];

                    break;
                case "季度":
                    unit = "Q";
                    dt = memInfo.GetList("select  convert(varchar,DATEPART (YYYY , selltime ))+ '年' + convert(varchar,DATEPART (Q , selltime )) + '季度' as dateU,sum(goodscount) as 数量 from goodsinfo where gid = " + cbogoods.SelectedValue + " and  selltime>='" + dtpStart.Value + "' and selltime<='" + dtpEnd.Value + "' group by DATEPART (YYYY , selltime ),DATEPART (Q , selltime ) order by DATEPART (YYYY , selltime ),DATEPART (Q , selltime )").Tables[0];

                    break;
                case "年":
                    unit = "YYYY";
                    dt = memInfo.GetList("select  DATEPART (YYYY , selltime ) dateU,sum(goodscount) as 数量 from goodsinfo where gid = " + cbogoods.SelectedValue + " and selltime>='" + dtpStart.Value + "' and selltime<='" + dtpEnd.Value + "' group by DATEPART (YYYY , selltime )").Tables[0];

                    break;
                default:
                    unit = "DD";
                    dt = memInfo.GetList("select  convert(varchar,selltime ) dateU,sum(goodscount) as 数量 from goodsinfo where gid = " + cbogoods.SelectedValue + " and selltime>='" + dtpStart.Value + "' and selltime<='" + dtpEnd.Value + "' group by selltime").Tables[0];
                    break;
            }
            chart1.DataSource = null;
            //renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); 
            chart1.DataSource = dt;
            //Axis a = this.
            
            chart1.Series["销售数量"].XValueMember = "dateU";
            chart1.Series["销售数量"].YValueMembers = "数量";
            chart1.Series["销售数量"].IsValueShownAsLabel = true;
            chart1.DataBind();

            chart1.ChartAreas["ChartArea1"].AxisX.Title = cbxDateUnit.Text;
            dgvDate.DataSource = dt;

            
            



            if (dt.Rows.Count > 9) //当X轴超过9时就会显示不完全
            {
                //设置X轴坐标的间隔为1
                chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
                //设置X轴坐标的偏移为1
                chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;
                //设置交错显示，例如数据多时可分两行显示
                chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsStaggered = true;
            }


        }
        private void btnsubmit_Click(object sender, EventArgs e)
        {
            sousuo();
        }
        public void jilian() {
            goods g = new goods();
            try
            {
                List<Model.goods> gg = g.GetModelList("goodstypeid = " + this.cboGoodsType.SelectedValue);
                if (gg.Count<=0)
                {
                    MessageBox.Show("没有该分类的商品！");
                    return;
                }
                this.cbogoods.DataSource = gg;
            }
            catch (Exception)
            {
                
                return;
            }

            this.cbogoods.DisplayMember = "goodsName";
            this.cbogoods.ValueMember = "goodsId";
        }

        private void cboGoodsType_SelectedIndexChanged(object sender, EventArgs e)
        {
            jilian();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }

        



    }
}
